package com.hc.server.actions;

import java.io.IOException;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hc.server.Error;
import com.hc.server.IAction;
import com.hc.server.Request;
import com.hc.server.Response;
import com.hc.server.conn.mall.MallResponse;
import com.hc.server.conn.mall.MallServerHelper;

public class QUERY_HOTELS implements IAction {

	private final static String CITY = "CITY";
	private final static String CHECKIN_DATE = "CHECKIN_DATE";
	private final static String CHECKOUT_DATE = "CHECKOUT_DATE";
	private final static String NAME = "NAME";
	private final static String LV = "LV";
	private final static String KEY = "KEY";
	private final static String SORT = "SORT";
	private final static String PAGE_INDEX = "PAGE_INDEX";
	private final static String PAGE_SIZE = "PAGE_SIZE";
	
	private final static String HID = "HID";
	private final static String ADD = "ADD";
//	private final static String USEAGE = "USEAGE";
//	private final static String DES = "HOTEL_DES";
//	private final static String	ROOMS = "ROOMS";
//	private final static String ROOM = "ROOM";
//	private final static String MPRICE = "MPRICE";	
	private final static String LPRICE = "LPRICE";
	private final static String PIC = "PIC";
//	private final static String STOCK = "STOCK";
	
	private final static String MALL_TOTAL_COUNT = "totalCount";
//	private final static String MALL_ = "rowsOfPage";
//	private final static String MALL_ = "totalPage";
//	private final static String MALL_ = "currentPage";
	private final static String MALL_LIST = "list";
	
	private final static String MALL_NAME = "name";
	private final static String MALL_PIC = "picUri";
	private final static String MALL_HID = "id";
	private final static String MALL_LV = "level";
	private final static String MALL_ADD = "address";
	private static final String MALL_SALE_PRICE = "localPrice";
	
	private final static String HOTELS = "HOTELS";
	private final static String TOTAL_RESULT = "TOTAL_RESULT";
	
	@Override
	public void execute(Request req, Response resp) {
		//1.处理参数
		String city;
		String checkInDate;
		String checkOutDate;
		try{
			city = req.getStrArg(CITY);
			checkInDate = req.getStrArg(CHECKIN_DATE);
			checkOutDate = req.getStrArg(CHECKOUT_DATE);
		}catch (Exception e) {
			resp.setErrResp(Error.INVALID_ARGS_ERR_CD, Error.INVALID_ARGS_ERR_MSG);
			return;
		}
		//可选参数
		int lv = req.optIntArg(LV);
		String name = req.optStrArg(NAME);
		String key = req.optStrArg(KEY);
		int orderBy = req.optIntArg(SORT);
		int pageIndex = req.optIntArg(PAGE_INDEX);
		pageIndex = pageIndex == 0 ? 1 : pageIndex;
		int pageSize = req.optIntArg(PAGE_SIZE);
		pageSize = pageSize == 0 ? 20 : pageSize;
		
		//2.查询结果
		MallResponse mResp = null;
		try{
			mResp = MallServerHelper.queryHotels(city, checkInDate, checkOutDate, name, lv, key, orderBy, pageIndex, pageSize);
		}catch (IOException e) {
			e.printStackTrace();
			resp.setErrResp(Error.MALLSERVER_NET_ERR_CD, Error.MALLSERVER_NET_ERR_MSG);
			return;
		} catch (JSONException e) {
			e.printStackTrace();
			resp.setErrResp(Error.MALLSERVER_DATA_FORMAT_ERR_CD, Error.MALLSERVER_DATA_FORMAT_ERR_MSG);
			return;
		}
		if(0 != mResp.getRetCode()){
			resp.setErrResp(Error.MALLSERVER_ERR_CD, mResp.getErrorMsg());
			return;
		}
		
		//3.返回结果
		try{
			JSONObject result = mResp.getResult();
			JSONArray hotels = new JSONArray();
			JSONArray list = result.getJSONArray(MALL_LIST);//应该使用get方式
			if(0 != list.length()){
				for(int index = 0; index < list.length(); index++){
					JSONObject tmp = list.getJSONObject(index);
					JSONObject hotel = new JSONObject();
					hotel.put(HID, tmp.getInt(MALL_HID));
					hotel.put(NAME, tmp.getString(MALL_NAME));
					hotel.put(LV, tmp.getInt(MALL_LV));
					hotel.put(KEY, "");
					hotel.put(PIC, tmp.getString(MALL_PIC));
					hotel.put(ADD, tmp.getString(MALL_ADD));
					hotel.put(LPRICE, tmp.getInt(MALL_SALE_PRICE));
					
					hotels.put(hotel);
				}
			}
			resp.appendRespContent(HOTELS, hotels);
			resp.appendRespContent(TOTAL_RESULT, result.getInt(MALL_TOTAL_COUNT));
		}catch (JSONException e) {
			e.printStackTrace();
			resp.setErrResp(Error.SERVER_SYS_ERR_CD, Error.SERVER_SYS_ERR_MSG);
			return;
		}

	}

}
